﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HomeStayWebCP.Models;
using DAL;
using HomeStayWebCP.Models.BusinessObject;
using HomeStayWebCP.Utility;
namespace HomeStayWebCP.Controllers
{
    public class WishlistsController : Controller
    {
        //
        // GET: /Wishlists/

        public ActionResult Index()
        {
            return PartialView("ViewWishList");
        }
        public ActionResult ViewWishList()
        {
            string username;
            if (HttpContext.Session["username"] != null && !HttpContext.Session["username"].ToString().Equals(""))
            {
                username = HttpContext.Session["username"].ToString();
                PostModel pmodel = new PostModel();
                List<WishListItem> list;
                list = pmodel.getWishListOfUser(username);
                if (list.Count != 0)
                {
                    ViewBag.WishList = list;
                }
                else
                {
                    ViewBag.WishList = null;
                }
            }
            else
            {
                //do nothing or later here but if View, UserID in session must =! null
            }

            return View();
        }
        public ActionResult DeleteWishListItem(int postID)
        {
            
            if (HttpContext.Session["username"] != null && !HttpContext.Session["username"].ToString().Equals(""))
            {
                PostModel model = new PostModel();
                
                string username = HttpContext.Session["username"].ToString();
                bool deleted = model.deleteWishListOfUser(postID, username);
                PostModel pmodel = new PostModel();
                List<WishListItem> list;
                list = pmodel.getWishListOfUser(username);
                if (list.Count != 0)
                {
                    ViewBag.WishList = list;
                }
                else
                {
                    ViewBag.WishList = null;
                }
            }
            else
            {
                //do nothing or later here but if View, UserID in session must =! null
            }
            return PartialView("ViewWishList");
        }

        #region Hoangdx Code Wishlist

        public ActionResult AddWistList()
        {
            string username = (HttpContext.Session["username"].ToString());
            int postID = (int.Parse(HttpContext.Session["PostID"].ToString()));

            WishlistModel model = new WishlistModel();
            bool check = model.AddWistList(postID, username);

            if (Request.IsAjaxRequest())
            {
                return Json(check, "application/json");//gui du lieu tra ve view .bien chuyen la count
            }
            else
            {
                return PartialView("ViewWishList");
            }
        }

        #endregion
    }
}
